package com.example.democonfigserver.modules.app;

import com.example.democonfigserver.modules.config.Config;
import com.example.democonfigserver.modules.project.Project;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.LinkedHashSet;
import java.util.Set;

import static com.example.democonfigserver.constant.ValidationConstant.REGEX_CODE;

/**
 * 应用
 *
 * @author wangbin
 */
@Entity
@Table(name = "system_application", indexes = {@Index(name = "code", columnList = "code", unique = true)})
@Getter
@Setter
public class App {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotBlank(message = "\"应用名\" 不能为空")
    private String name;

    @Pattern(regexp = REGEX_CODE, message = "\"应用编号\" 只能是 数字 、 字母和 \"-\"")
    @NotBlank(message = "\"应用编号\" 不能为空")
    private String code;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "system_application_configuration",
            joinColumns = @JoinColumn(name = "configuration_id"),
            inverseJoinColumns = @JoinColumn(name = "application_id"))
    @OrderBy("id asc")
    @JsonDeserialize(as = LinkedHashSet.class)
    @JsonIgnoreProperties(value = {"app", "appList"}, allowSetters = true)
    private Set<Config> configList = new LinkedHashSet<>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id")
    private Project project;

}
